package jupiter.util;
import java.util.*;

public class Sync {
	public static Hashtable<String, ArrayList<String>> d = new Hashtable<String, ArrayList<String>>();
	public static Hashtable<String, String> current = new Hashtable<String, String>();
	public static boolean _print = false;
	public static void s(Object o, String m){
		print("s> "+o+" "+o.getClass().getSimpleName()+":"+m);
		synchronized (d) {
			if(!d.containsKey(o.toString()))
				d.put(o.toString(), new ArrayList<String>());
			ArrayList<String> waiting = d.get(o.toString());
			for(String w : waiting)
				print("  \\ "+w);
			waiting.add(val(o,m));
			if(current.containsKey( o.toString() ))
				print("   Waiting on = "+current.get(o.toString()));
		}
	}
	public static String val(Object o, String m){
		return o.getClass().getSimpleName()+":"+m;
	}
	public static void r(Object o, String m){
		synchronized (d) {
			current.put( o.toString() , val(o,m)   );
			
			if(!d.containsKey(o.toString()))
				d.put(o.toString(), new ArrayList<String>());
			ArrayList<String> waiting = d.get(o.toString());
			waiting.remove(val(o,m));
		}
		if(_print)
			print("r= "+o+" "+o.getClass().getSimpleName()+":"+m);
	}
	/**
	 * when done, remove as waiting
	 * @param o
	 * @param m
	 */
	public static void u(Object o, String m){
		if(_print)
			print("u< "+o+" "+o.getClass().getSimpleName()+":"+m);
		synchronized (d) {
			current.remove( o.toString() );
		}
	}
	public static void  print(String s){
		if(_print)
			System.out.println(s);
	}
}
